/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.CFields;
import DTO.Folder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FolderBUS extends IData<Folder>{

    @Override
    public Folder add(Folder item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Folder item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Folder> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Folder Find(Folder item, List<Folder> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(Folder item, int t, SubConfig config) {
       Object result = 0;
        String mysql ="INSERT INTO folders(" + CFields.name+ "," + CFields.description +","+CFields.parents+","+CFields.path_files+","+CFields.type+","+CFields.create_date+")" + " VALUES (?,?,?,?,?,?);";                      
        try {   
            PreparedStatement cs;
            if(t == 0){                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
                cs.setString(1,item.getName());
                cs.setString(2,item.getDescription());
                cs.setLong(3,item.getParents());
                cs.setString(4,item.getPath_files());
                cs.setString(5, item.getType());
                cs.setTimestamp(6, item.getCreate_date());
                //cs.setBoolean(7,item.getIs_folder());
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t== 1){
                mysql = "update folders set " + CFields.name + "=?,"
                        + CFields.description + "=?,"+CFields.parents+"=?,"+CFields.path_files+"=?,"+CFields.type+"=?,"+CFields.create_date+"=?, where " + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setString(1,item.getName());
                cs.setString(2,item.getDescription());
                cs.setLong(3,item.getParents());
                cs.setString(4,item.getPath_files());
                cs.setString(5, item.getType());
                cs.setTimestamp(6, item.getCreate_date());
                cs.setLong(7, item.getId());
                result = cs.executeUpdate();
            }
            else{
                
                mysql = "delete from folders where "+ CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } catch (SQLException ex) {            
            Logger.getLogger(FolderBUS.class.getName()).log(Level.SEVERE, null, ex);            
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(Folder item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Folder item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Folder item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected Folder getItem(ResultSet rs) {
        Folder item = new Folder();
        try{
            item.setDescription(rs.getString(CFields.description));
            item.setName(rs.getString(CFields.name));
            item.setId(rs.getLong(CFields.id));
            item.setParents(rs.getLong(CFields.parents));
            item.setUser_id(rs.getLong(CFields.user_id));
            item.setPath_files(rs.getString(CFields.path_files));
            item.setType(rs.getString(CFields.type));
        }
        catch(Exception error){
        }
        return item;
    }

    @Override
    public List<Folder> getAll(SubConfig config) {
        String query = "select * from " + CFields.tb_folders + " where isdelete =false";
        return this.getAll(query, config);
    }
    
}
